/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework
 * Version 2.10
 * 
 * Application Database Factory Class
 *      Provides static methods for generating the underlying data access
 * implementation based on application settings.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.Data;
using System.Data.SqlClient;
using Adaptive.Foundation.Properties;

namespace Adaptive.Foundation.Data
{
    /// <summary>
    /// Provides static methods for generating the underlying data access
    /// implementation based on application settings.
    /// </summary>
    public static class ApplicationDatabaseFactory
    {
        /// <summary>
        /// Creates the data actor instance specified by the application settings.
        /// </summary>
        /// <returns>
        /// A <see cref="DataActorBase"/> instance.
        /// </returns>
        public static DataActorBase CreateDataActor()
        {
            Settings appSettings = null;                                //The current application settings.
            DataActorBase returnValue = null;                           //The return value.
            DataSourceType sourceType = DataSourceType.SqlServer;       //The data source type value.

            //Read the type of datbase being used from the application settings.
            appSettings = new Settings();
            sourceType = (DataSourceType)appSettings.DatabaseType;

            //Create the objects based on database type.
            switch (sourceType)
            {
                case DataSourceType.SqlServer:
                    returnValue = new SqlDataActor(appSettings.DatabaseConnectString);
                    break;

                case DataSourceType.OleDB:
                    returnValue = new OleDBDataActor(appSettings.DatabaseConnectString);
                    break;

                case DataSourceType.Odbc:
                    returnValue = new OdbcDataActor(appSettings.DatabaseConnectString);
                    break;

                default:
                    break;
            }

            return returnValue;
        }
        /// <summary>
        /// Tests the ability to connect using the current settings.
        /// </summary>
        /// <returns>
        /// <b>true</b> if a database connection can be made; otherwise, <b>false</b>.
        /// </returns>
        public static bool TestConnectivitySettings()
        {
            DataActorBase actor = null;
            bool returnValue = false;

            actor = CreateDataActor();
            if (actor != null)
            {
                returnValue = actor.TestConnectivity();
                actor.Dispose();
            }
            return returnValue;
        }
    }
}
